package com.tongfusoft.document.manager.metadata.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.dto.IpFieldsetsDto;
import com.tongfusoft.document.entity.IpFieldsets;
import com.tongfusoft.document.entity.MetadataDefine;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.metadata.service.IMetadataService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("metaDataManagerService")
public class MetadataServiceImpl extends DataTemplateServiceImpl
		implements IMetadataService {
	
	@Autowired
	@Qualifier("metaDataManagerService")
	public IMetadataService metaDataManagerService;
	

	@Override
	public void createMetadata(MetadataDefine define, Users user) {
		if (this.selectList("getMetadataByName", define).size() > 0  ) {

			throw new ServiceException("repeat_error");
		}

		define.setMetadataId(UIDGeneratorService.getUUID());
		define.setCreatedBy(user.getUserId());
		define.setCreatedDate(new Timestamp(new Date().getTime()));
		define.setLastUpdBy(user.getUserId());
		define.setLastUpdDate(new Timestamp(new Date().getTime()));
		define.setStatus("1");
		define.setIsEnable("0");
		this.insert("createMetadata", define);

	}

	@Override
	public void updateMetadata(MetadataDefine define,String metadataId ,Users user) {
		
		MetadataDefine newDefine = new MetadataDefine();
		newDefine.setMetadataId(metadataId);
		
		MetadataDefine defineUp = this.metaDataManagerService.getMetadata(newDefine);
		defineUp.setMetadataName(define.getMetadataName());
		defineUp.setSelectData(define.getSelectData());
		defineUp.setMetaSort(define.getMetaSort());
		defineUp.setMetadataNote(define.getMetadataNote());
		defineUp.setLastUpdBy(user.getUserId());
		defineUp.setLastUpdDate(new Timestamp(new Date().getTime()));
		
		if (this.selectList("getMetadataByNameOutSelf", defineUp).size() > 0 ) {

			throw new ServiceException("repeat_error");
		}
		
		this.update("updateMetadata", defineUp);

	}

	@Override
	public void deleteMetadata(MetadataDefine define, Users user) {
		MetadataDefine deleteMetadata = this.metaDataManagerService.getMetadata(define);
		
		deleteMetadata.setCreatedBy(user.getUserId());
		deleteMetadata.setCreatedDate(new Timestamp(new Date().getTime()));
		deleteMetadata.setLastUpdBy(user.getUserId());
		deleteMetadata.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.update("deleteMetadata", deleteMetadata);

	}

	@Override
	public MetadataDefine getMetadata(MetadataDefine define) {
		MetadataDefine define2 = (MetadataDefine) this.selectOne("getMetadata",define);
		return define2;
	}

	@Override
	public PageInfo metadataList(int pageSize, int pageIndex,
			MetadataDefine define) {
		return super.findPage("metadataList", define, pageSize, pageIndex);
	}

	@Override
	public List<MetadataDefine> getIsEnableMeta() {
		// TODO Auto-generated method stub
		return super.selectList("getIsEnableMeta", new HashMap());
	}

	@Override
	public List<IpFieldsetsDto> getIsEnableMetaByProFile(String profileId) {
		// TODO Auto-generated method stub
		List<IpFieldsets> fileSetList = this.selectList("validProfieldset",
				profileId);
		List<IpFieldsetsDto> fmDtoList = new ArrayList();
		for (IpFieldsets fileSet : fileSetList) {

			IpFieldsetsDto fs = new IpFieldsetsDto();
			fs.setFieldsetId(fileSet.getFieldsetId());

			List<Map> mapList = this.selectList("getIsEnableMetaByFieldSet",
					fileSet.getFieldsetId());
			fs.setMetaDataDtoList(mapList);
			fmDtoList.add(fs);
		}

		return fmDtoList;
	}

	@Override
	public void updateIsEnable(MetadataDefine define, String isEnable,Users user) {
		MetadataDefine defineUp = this.metaDataManagerService.getMetadata(define);
		defineUp.setIsEnable(isEnable);
		defineUp.setLastUpdBy(user.getUserId());
		defineUp.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.update("updateMetadata", defineUp);
		
	}

}
